//
//  DCMineBankCardController.m
//  Demo
//
//  Created by zhaolin on 2023/1/4.
//

#import "DCMineBankCardController.h"
#import "DCBankCardCell.h"
#import "DCBankCardModel.h"
#import "DCChangeBankCardController.h"


@interface DCMineBankCardController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) UILabel *bankName;

@property (nonatomic, strong) UILabel *bankAccount;

@property (nonatomic, strong) UIButton *guanlian;

@property (nonatomic, strong) DCBankCardModel *cardModel;
@end

@implementation DCMineBankCardController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitleName:@"Bank card"];
    [self getDataFromNet];
    // Do any additional setup after loading the view.
}

- (void)configureAlloc {
    [super configureAlloc];
}

- (void)configureUI {
    [super configureUI];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    [self.tableView registerClass:DCBankCardCell.class forCellReuseIdentifier:NSStringFromClass(DCBankCardCell.class)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = self.view.backgroundColor ;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.navgitBar.mas_bottom);
        make.left.right.bottom.mas_equalTo(0);
    }];
    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 260)];
    self.tableView.tableHeaderView = headerView;
    
    UIView *cardView = [UIView new];
    cardView.backgroundColor = [UIColor clearColor];
    [headerView addSubview:cardView];
    [cardView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.height.mas_equalTo(210);
    }];
    
    UIImageView *cardImage = [[UIImageView alloc] init];
    cardImage.userInteractionEnabled = YES;
//    cardImage.backgroundColor = [UIColor colorWithString:@"#F0FFCE"];
    cardImage.image = [UIImage imageNamed:@"cardImage"];
    [cardView addSubview:cardImage];
    [cardImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30);
        make.right.mas_equalTo(-30);
        make.top.mas_equalTo(30);
        make.height.mas_equalTo(156);
    }];
    
    UILabel *bankName = [UILabel new];
    _bankName = bankName;
//    bankName.backgroundColor = [UIColor redColor];
    bankName.numberOfLines = 2;
    bankName.text = @"--";
    bankName.textColor = [UIColor colorWithString:@"#FFFFFF"];
    bankName.font = DCFont_PF_Medium(16);
    [cardImage addSubview:bankName];
    [bankName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-100);
        make.left.mas_equalTo(38);
        make.top.mas_equalTo(19);
    }];
    
    UILabel *bankAccount = [UILabel new];
    _bankAccount = bankAccount;
    bankAccount.text = @"--";
    bankAccount.textColor = [UIColor colorWithString:@"#1C221B"];
    bankAccount.font = DCFont_PF_Semibold(16);
    [cardImage addSubview:bankAccount];
    [bankAccount mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(38);
        make.bottom.mas_equalTo(-32);
    }];
    
    UIButton *modifyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    modifyBtn.layer.cornerRadius = 4;
    modifyBtn.layer.masksToBounds = YES;
    modifyBtn.backgroundColor = [UIColor colorWithString:@"#1C221B"];
    [modifyBtn setTitle:@"Modify >" forState:UIControlStateNormal];
    [modifyBtn addTarget:self action:@selector(modifyAction:) forControlEvents:UIControlEventTouchUpInside];
    [modifyBtn setTitleColor:[UIColor colorWithString:@"#FFFFFF"] forState:UIControlStateNormal];
    modifyBtn.titleLabel.font = DCFont_PF_Medium(14);
    [cardImage addSubview:modifyBtn];
    [modifyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-28);
        make.bottom.mas_equalTo(-25);
        make.width.mas_equalTo(92);
        make.height.mas_equalTo(32);
    }];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    _guanlian = button;
    [button setTitle:@" Associated products" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor colorWithString:@"#1C221B"] forState:UIControlStateNormal];
    button.titleLabel.font = DCFont_PF_Medium(14);
    [button setImage:[UIImage imageNamed:@"guanlian"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(guanlianActioin:) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(cardView.mas_bottom).mas_offset(7);
        make.centerX.mas_equalTo(cardView);
    }];
}

- (void)setUpHeaderData {
    _bankName.text = self.cardModel.data.bankName;
    _bankAccount.text = self.cardModel.data.bankCode;
}

- (void)getDataFromNet {
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
//    mWeakSelf
    [DCHttpRequest DCBankCardListCompleteBlock:^(NSURLSessionDataTask * _Nonnull operation, DCResponseData * _Nonnull responseObject) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (responseObject.code == 200) {
            if ([responseObject.data isKindOfClass:[NSArray class]]) {
                NSDictionary *dataDic = [(NSArray *)responseObject.data firstObject];
                self.cardModel = [DCBankCardModel mj_objectWithKeyValues:dataDic];
            } else {
                self.cardModel = [DCBankCardModel mj_objectWithKeyValues:responseObject.data];
            }
            [self setUpHeaderData];
            [self.tableView reloadData];
        } else {
            [DCDCToast toastText:responseObject.message];
        }
    }];
}
- (void)modifyAction:(UIButton *)sender {
    DCChangeBankCardController *changeVC = [DCChangeBankCardController new];
    changeVC.cardModel = self.cardModel;
    [self.navigationController pushViewController:changeVC animated:YES];
}

- (void)guanlianActioin:(UIButton *)sender {
    
}
#pragma mark - tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.cardModel.productNameAndSloganList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DCBankCardCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(DCBankCardCell.class)];
    DCProductModel * model =[self.cardModel.productNameAndSloganList objectAtIndex:indexPath.row];
    [cell setData:model];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 74;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   
}

@end
